package com.cloud.iotdb.utils.wrapper;

import com.cloud.iotdb.utils.IotDBDaoImp;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JohnCoding
 * @Description: 拼装IotDB查询条件的Wrapper
 * @date: 2020/3/25 11:32
 */
public abstract class BaseIotWrapper {

    @Autowired
    IotDBDaoImp iotDBDaoImp;

    /**
     * 总的sql
     */
    protected StringBuffer sql = new StringBuffer();

    /**
     * where 语句
     */
    protected StringBuffer where = new StringBuffer();

    /**
     * 是否是last语法
     */
    protected boolean islast = false;

    /**
     * 查询的时间序列
     */
    protected List<String> timeseries = new ArrayList<>();

    /**
     * 一直到设备的url
     */
    protected String url;


    public BaseIotWrapper() {
    }

    /**
     * and
     * @return
     */
    public abstract BaseIotWrapper and();


    /**
     * or
     * @return
     */
    public abstract BaseIotWrapper or();


    /**
     * 小于等于 <=
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper le(String timeseriesName, Object val);

    /**
     * 等于 =
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper eq(String timeseriesName, Object val);

    /**
     * 不等于 <>
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper ne(String timeseriesName, Object val);

    /**
     * 大于 >
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper gt(String timeseriesName, Object val);

    /**
     * 大于等于 >=
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper ge(String timeseriesName, Object val);

    /**
     * 小于等于 <
     * @param timeseriesName 时间序列名称
     * @param val 值
     * @return
     */
    public abstract BaseIotWrapper lt(String timeseriesName, Object val);


    /**
     * 无视sql规则直接拼接到最后
     * @return
     */
    public abstract BaseIotWrapper last();


    /**
     * 查询时间序列
     * @param timeseries
     * @return
     */
    public abstract BaseIotWrapper select(String... timeseries);


    public StringBuffer getSql() {
        return sql;
    }

    public void setSql(StringBuffer sql) {
        this.sql = sql;
    }

    public StringBuffer getWhere() {
        return where;
    }

    public void setWhere(StringBuffer where) {
        this.where = where;
    }

//    public List<String> getTimeseries() {
//        if( null == timeseries || 0 ==  timeseries.size()){
//            List<TimeseriesDomain> times = iotDBDaoImp.getTimeseriesByDeviceId(deviceId);
//            if(null == times || 0 == times.size()){
//                timeseries.add(" * ");
//            }
//            for (TimeseriesDomain time : times) {
//                timeseries.add(String.valueOf(time.getName()));
//            }
//        }
//        return timeseries;
//    }

    public void setTimeseries(List<String> timeseries) {
        this.timeseries = timeseries;
    }

    public List<String> getTimeseries() {
        return timeseries;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
